package com.hospital.service.impl;

import com.hospital.dao.UserDao;
import com.hospital.entity.User;
import com.hospital.service.LoginService;
import com.hospital.vo.ResponseVO;
import com.hospital.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author yuhuan
 * @Date 2022/3/23
 */
@Slf4j
@DubboService
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserDao userDao;

    /**
     * 添加（注册）用户
     * @param userVO
     * @return
     */
    @Override
//    @Transactional(rollbackFor = {Exception.class})
    public ResponseVO insert(UserVO userVO) {
        log.debug(">>>>>>注册用户");
        User user = new User();
        BeanUtils.copyProperties(userVO,user);

        try {
            String email = user.getEmail();
            String check = "[1-9]\\d{7,15}@qq\\.com";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            boolean matchers = matcher.matches();

            String userAccount = user.getUserAccount();
            String check1 = "^[A-Za-z0-9]+$";
            Pattern regex1 = Pattern.compile(check1);
            Matcher matcher1 = regex1.matcher(userAccount);
            boolean matchers1 = matcher1.matches();

            String password1 = user.getUserPasswrod();
            String password2 = user.getUserPasswrod2();
            if(!matchers){
                return ResponseVO.buildSuccessInstance(ResponseVO.EMAIL_FORMAT);
            }
            if(!matchers1){
                return ResponseVO.buildSuccessInstance(ResponseVO.NUMBER_ACCOUNT);
            }
            if(!password2.equals(password1)){
                return ResponseVO.buildSuccessInstance(ResponseVO.PASSWORD_ERROR);
            }
            String account = user.getUserAccount();
            userDao.getName(account);
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseVO.buildSuccessInstance(ResponseVO.EXIST_ACCOUNT);
        }
        Integer userId = userDao.insert(user);
        if(userId>0){
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
        }
        return ResponseVO.buildSuccessInstance(ResponseVO.UPDATE_FAIL);
    }

    /**
     * 根据Account账号跟Password密码来登录
     * @return
     */
    @Override
    public ResponseVO login(UserVO userVO) {
        log.debug(">>>>>>登录");
        User user = new User();
        BeanUtils.copyProperties(userVO,user);
        User ss = userDao.login(user);
        if(ss==null){
            return ResponseVO.buildSuccessInstance(ResponseVO.NULL_RESULT);
        }else {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
        }

    }

    @Override
    public ResponseVO findUserById(int userId) {
        return ResponseVO.buildSuccessInstance(ResponseVO.NULL_RESULT);
    }
}
